#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sensor_data.pb-c.h"

// 模拟 GPRS 发送函数
void send_gprs(uint8_t *buffer, size_t len) {
    printf("Sending %zu bytes via GPRS: ", len);
    for (size_t i = 0; i < len; i++) {
        printf("0x%02x, ", buffer[i]);
    }
    printf("\n");
}

int main() {
    // 初始化 SensorDataList
    Sensor__SensorDataList msg_list = SENSOR__SENSOR_DATA_LIST__INIT;
    uint8_t *buffer;
    size_t len;

    // 定义两个传感器数据：一个温度，一个湿度
    Sensor__UnifiedSensorData sensor1 = SENSOR__UNIFIED_SENSOR_DATA__INIT;
    Sensor__UnifiedSensorData sensor2 = SENSOR__UNIFIED_SENSOR_DATA__INIT;

    // 传感器 1：温度传感器
	sensor1.has_id = 1;
    sensor1.id = 101;
    sensor1.name = strdup("TempSensor");
    Sensor__TemperatureData temp_data = SENSOR__TEMPERATURE_DATA__INIT;
	temp_data.has_temperature = 1;
    temp_data.temperature = 25.5;
    sensor1.sensor_value_case = SENSOR__UNIFIED_SENSOR_DATA__SENSOR_VALUE_TEMPERATURE_DATA;
    sensor1.temperature_data = &temp_data;

    // 传感器 2：湿度传感器
	sensor2.has_id = 1;
    sensor2.id = 102;
    sensor2.name = strdup("HumidSensor");
    Sensor__HumidityData humid_data = SENSOR__HUMIDITY_DATA__INIT;
	humid_data.has_humidity = 1;
    humid_data.humidity = 60.0;
    sensor2.sensor_value_case = SENSOR__UNIFIED_SENSOR_DATA__SENSOR_VALUE_HUMIDITY_DATA;
    sensor2.humidity_data = &humid_data;

    // 创建 items 数组
    Sensor__UnifiedSensorData *items[2];
    items[0] = &sensor1;
    items[1] = &sensor2;

    // 设置 msg_list
    msg_list.n_items = 2;
    msg_list.items = items;

    // 序列化
    len = sensor__sensor_data_list__get_packed_size(&msg_list);
    buffer = malloc(len);
    if (!buffer) {
        fprintf(stderr, "Memory allocation failed\n");
        return 1;
    }
    sensor__sensor_data_list__pack(&msg_list, buffer);

    // 发送
    send_gprs(buffer, len);

    // 清理内存
    free(sensor1.name);
    free(sensor2.name);
    free(buffer);
    return 0;
}